//
//  YDBaseViewController.h
//  IngCard
//
//  Created by kf on 2017/7/10.
//  Copyright © 2017年 IngDan. All rights reserved.
//


#import "QMUICommonDefines.h"
#import "QMUIConfiguration.h"
#import "QMUIHelper.h"
#import "QMUINavigationTitleView.h"
#import "QMUIEmptyView.h"
#import "NSString+QMUI.h"
#import "YDBaseViewController.h"

@interface YDBaseViewController ()

@property(nonatomic,strong,readwrite) QMUINavigationTitleView *titleView;
@property(nonatomic,strong) QMUIEmptyView *emptyView;
@property(nonatomic, assign) BOOL *dismissClearTasks;
@end

@implementation YDBaseViewController

#pragma mark - 生命周期

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self didInitialized];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self didInitialized];
    }
    return self;
}

- (void)didInitialized {
    self.hidesBottomBarWhenPushed = HidesBottomBarWhenPushedInitially;
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.dismissClearTasks = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorForBackground;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    if ([self respondsToSelector:@selector(initNBandSB)])
    {
        [self initNBandSB];
    }
    [self initSubviews];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    if (self.dismissClearTasks && (self.currentTasks.count > 0))
    {
        [self.currentTasks enumerateObjectsUsingBlock:^(NSURLSessionTask * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj cancel];
        }];
        [self.currentTasks removeAllObjects];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self layoutEmptyView];
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 空列表视图 QMUIEmptyView

- (void)showDebugInfo
{
    Class debugCls = NSClassFromString(@"UIDebuggingInformationOverlay");
    [debugCls performSelector:NSSelectorFromString(@"prepareDebuggingOverlay")];
    
    Class someClass = NSClassFromString(@"UIDebuggingInformationOverlay");
    id obj = [someClass performSelector:NSSelectorFromString(@"overlay")];
    [obj performSelector:NSSelectorFromString(@"toggleVisibility")];
}


- (void)showEmptyView
{
    [self.view addSubview:self.emptyView];
}

- (void)hideEmptyView
{
    [self.emptyView removeFromSuperview];
}

- (BOOL)isEmptyViewShowing
{
    return self.emptyView.superview;
}

- (void)showEmptyViewWithLoading {
    [self showEmptyView];
    [self.emptyView setImage:nil];
    [self.emptyView setLoadingViewHidden:NO];
    [self.emptyView setTextLabelText:nil];
    [self.emptyView setDetailTextLabelText:nil];
    [self.emptyView setActionButtonTitle:nil];
}

- (void)showEmptyViewWithText:(NSString *)text
                   detailText:(NSString *)detailText
                  buttonTitle:(NSString *)buttonTitle
                 buttonAction:(SEL)action {
    [self showEmptyViewWithLoading:NO image:nil text:text detailText:detailText buttonTitle:buttonTitle buttonAction:action];
}

- (void)showEmptyViewWithImage:(UIImage *)image
                          text:(NSString *)text
                    detailText:(NSString *)detailText
                   buttonTitle:(NSString *)buttonTitle
                  buttonAction:(SEL)action {
    [self showEmptyViewWithLoading:NO image:image text:text detailText:detailText buttonTitle:buttonTitle buttonAction:action];
}

- (void)showEmptyViewWithLoading:(BOOL)showLoading
                           image:(UIImage *)image
                            text:(NSString *)text
                      detailText:(NSString *)detailText
                     buttonTitle:(NSString *)buttonTitle
                    buttonAction:(SEL)action {
    [self showEmptyView];
    [self.emptyView setLoadingViewHidden:!showLoading];
    [self.emptyView setImage:image];
    [self.emptyView setTextLabelText:text];
    [self.emptyView setDetailTextLabelText:detailText];
    [self.emptyView setActionButtonTitle:buttonTitle];
    [self.emptyView.actionButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
    [self.emptyView.actionButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Private

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    self.titleView.title = title;
}

- (BOOL)layoutEmptyView {
    if (self.emptyView) {
        // 由于为self.emptyView设置frame时会调用到self.view，为了避免导致viewDidLoad提前触发，这里需要判断一下self.view是否已经被初始化
        BOOL viewDidLoad = self.emptyView.superview || [self isViewLoaded];
        if (viewDidLoad) {
            CGSize newEmptyViewSize = self.emptyView.superview.bounds.size;
            CGSize oldEmptyViewSize = self.emptyView.frame.size;
            if (!CGSizeEqualToSize(newEmptyViewSize, oldEmptyViewSize)) {
                self.emptyView.frame = CGRectMake(CGRectGetMinX(self.emptyView.frame), CGRectGetMinY(self.emptyView.frame), newEmptyViewSize.width, newEmptyViewSize.height);
            }
            return YES;
        }
    }
    
    return NO;
}
-(void) initNBandSB
{
    
}

-(void) initSubviews
{
    
}

-(void) initDataSource
{
    
}

#pragma mark - Lazy Loading
-(QMUINavigationTitleView *) titleView
{
    if (!_titleView)
    {
        _titleView = [[QMUINavigationTitleView alloc] init];
        self.navigationItem.titleView = _titleView;
    }
    return _titleView;
}

-(QMUIEmptyView *)emptyView
{
    if (!_emptyView)
    {
        _emptyView = [[QMUIEmptyView alloc] initWithFrame:self.view.bounds];
    }
    return _emptyView;
}

-(NSMutableArray<NSURLSessionTask *> *) currentTasks
{
    if(!_currentTasks)
    {
        _currentTasks = [NSMutableArray arrayWithCapacity:5];
    }
    return _currentTasks;
}

@end
